package com.gopay.common.cps.manager.order.impl;

import com.gopay.common.cps.dao.order.CpsGeneralFundOrderQueryDAO;
import com.gopay.common.cps.dao.order.CpsGeneralMainOrderQueryDAO;
import com.gopay.common.cps.dao.order.CpsGeneralReceiptOrderQueryDAO;
import com.gopay.common.cps.manager.order.FullCpsGenRecvOrderQueryManager;
import com.gopay.common.domain.cps.CpsGenMainOrder;
import com.gopay.common.domain.cps.CpsGenReceiptOrder;
import com.gopay.common.domain.cps.vo.FullCpsGenFundOrder;
import com.gopay.common.domain.cps.vo.FullCpsGenRecvOrder;
import com.gopay.common.domain.fund.FundGopayCpsGenOrder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 收款订单VO查询操作manager
 * 
 * @FullCpsGenRecvOrderQueryManagerImpl.java
 * @author MA HUAN
 * @2013-1-9 上午11:58:18 www.gopay.com.cn Inc.All rights reserved.
 */
@Service(value = "fullCpsGenRecvOrderQueryManager")
public class FullCpsGenRecvOrderQueryManagerImpl implements FullCpsGenRecvOrderQueryManager {
    /** 带参数查询订单：担保支付取消通过预授权号查询担保支付申请订单 */
    private final String FIND_ORG_FULL_RECV_ORDER_HQL = "FROM CpsGenReceiptOrder r WHERE r.authId =:authId ";
    private final String FIND_FULL_RECV_ORDER_BY_MERID_AND_MERORDERID_HQL = "FROM CpsGenMainOrder r WHERE r.orderType = '01' and r.merId =:merId and r.merOrderId=:merOrderId";
    private final String FIND_02_ORDER_BY_MERID_AND_MERORDERID_HQL = "FROM CpsGenMainOrder r WHERE r.merId =:merId and r.merOrderId=:merOrderId and r.txnStaCd = '20000'";
    private final String FIND_FULL_FUND_ORDER_BY_MERID_AND_MERORDERID_HQL = "FROM CpsGenMainOrder r WHERE r.orderType = '00' and r.merId =:merId and r.merOrderId=:merOrderId";

    @Resource(name = "cpsGeneralMainOrderQueryDAO")
    private CpsGeneralMainOrderQueryDAO cpsGeneralMainOrderQueryDAO;

    @Resource(name = "cpsGeneralReceiptOrderQueryDAO")
    private CpsGeneralReceiptOrderQueryDAO cpsGeneralReceiptOrderQueryDAO;

    @Resource(name = "cpsGeneralFundOrderQueryDAO")
    private CpsGeneralFundOrderQueryDAO cpsGeneralFundOrderQueryDAO;

    @Override
    public FullCpsGenRecvOrder get(Serializable id) {
        CpsGenMainOrder main = cpsGeneralMainOrderQueryDAO.find(CpsGenMainOrder.class, id);
        if (main != null) {
            CpsGenReceiptOrder recv = cpsGeneralReceiptOrderQueryDAO.find(CpsGenReceiptOrder.class, id);

            //解密
            //PCI 认证后废弃   DesUtilWithStockOfData.decrypt(recv, true);
            if (recv != null) {
                return new FullCpsGenRecvOrder(main, recv);
            }
        }
        return null;
    }

    @Override
    public List<FullCpsGenRecvOrder> get(Map<Object, Object> paraMap) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<FullCpsGenRecvOrder> getAll() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public FullCpsGenRecvOrder findFullCpsGenRecvOrder(String authId) {
        Map<String, Object> para = new HashMap<String, Object>();
        para.put("authId", authId);
        List<CpsGenReceiptOrder> recvList = cpsGeneralReceiptOrderQueryDAO.findByHql(this.FIND_ORG_FULL_RECV_ORDER_HQL,
                para);
        if (recvList != null && recvList.size() > 0) {
            CpsGenReceiptOrder recv = recvList.get(0);
            CpsGenMainOrder main = cpsGeneralMainOrderQueryDAO.find(CpsGenMainOrder.class, recv.getGopayOrderId());
            if (main != null) {
                return new FullCpsGenRecvOrder(main, recv);
            }
        }
        return null;
    }

    @Override
    public CpsGenMainOrder findByGopayOrderId(String gopayOrderId) {
        return cpsGeneralMainOrderQueryDAO.findByGopayOrderId(gopayOrderId);
    }

    @Override
    public List<FullCpsGenRecvOrder> findFullCpsGenRecvOrder(String merId, String merOrderId) {
        Map<String, Object> para = new HashMap<String, Object>();
        para.put("merId", merId);
        para.put("merOrderId", merOrderId);
        List<CpsGenMainOrder> recvList = cpsGeneralMainOrderQueryDAO.findByHql(
                this.FIND_FULL_RECV_ORDER_BY_MERID_AND_MERORDERID_HQL, para);
        List<FullCpsGenRecvOrder> list = new ArrayList<FullCpsGenRecvOrder>();
        if (recvList == null || recvList.size() == 0) {
            return list;
        }
        for (CpsGenMainOrder main : recvList) {
            CpsGenReceiptOrder recv = cpsGeneralReceiptOrderQueryDAO.find(CpsGenReceiptOrder.class,
                    main.getGopayOrderId());
            FullCpsGenRecvOrder f = new FullCpsGenRecvOrder(main, recv);
            list.add(f);
        }
        return list;
    }

    @Override
    public List<FullCpsGenFundOrder> findFullCpsGenFundOrder(String merId, String merOrderId) {
        Map<String, Object> para = new HashMap<String, Object>();
        para.put("merId", merId);
        para.put("merOrderId", merOrderId);
        List<CpsGenMainOrder> fundList = cpsGeneralMainOrderQueryDAO.findByHql(
                this.FIND_FULL_FUND_ORDER_BY_MERID_AND_MERORDERID_HQL, para);
        List<FullCpsGenFundOrder> list = new ArrayList<FullCpsGenFundOrder>();
        if (fundList == null || fundList.size() == 0) {
            return list;
        }
        for (CpsGenMainOrder main : fundList) {
            FundGopayCpsGenOrder fund = cpsGeneralFundOrderQueryDAO.find(FundGopayCpsGenOrder.class,
                    main.getGopayOrderId());
            FullCpsGenFundOrder f = new FullCpsGenFundOrder(main, fund);
            list.add(f);
        }
        return list;
    }

    @Override
    public FullCpsGenRecvOrder findSuccessFullCpsGenRecvOrder(String merId, String merOrderId) {
        Map<String, Object> para = new HashMap<String, Object>();
        para.put("merId", merId);
        para.put("merOrderId", merOrderId);
        List<CpsGenMainOrder> mainList = cpsGeneralMainOrderQueryDAO.findByHql(
                this.FIND_02_ORDER_BY_MERID_AND_MERORDERID_HQL, para); // 找状态是成功的订单
        if (mainList != null && mainList.size() > 0) {
            CpsGenMainOrder main = mainList.get(0);
            CpsGenReceiptOrder recv = cpsGeneralReceiptOrderQueryDAO.find(CpsGenReceiptOrder.class,
                    main.getGopayOrderId());
            if (recv != null) {
                return new FullCpsGenRecvOrder(main, recv);
            }
        }
        return null;
    }

    @Override
    public BigDecimal getTotalRefundAmt(String gopayOrderId) {
        BigDecimal total02RefundAmt = this.cpsGeneralReceiptOrderQueryDAO.findTotal02RefundAmt(gopayOrderId);
        BigDecimal total01RefundAmt = this.cpsGeneralMainOrderQueryDAO.findTotal01RefundAmt(gopayOrderId);
        if(total02RefundAmt != null && total01RefundAmt != null) {
            return total02RefundAmt.add(total01RefundAmt);
        }
        return null;
    }

    @Override
    public BigDecimal getTotalRefundFeeAmt(String gopayOrderId) {
        BigDecimal total02RefundFeeAmt = this.cpsGeneralReceiptOrderQueryDAO.findTotal02RefundFeeAmt(gopayOrderId);
        BigDecimal total01RefundFeeAmt = this.cpsGeneralMainOrderQueryDAO.findTotal01RefundFeeAmt(gopayOrderId);
        if(total02RefundFeeAmt != null && total01RefundFeeAmt != null) {
            return total02RefundFeeAmt.add(total01RefundFeeAmt);
        }
        return null;
    }

    @Override
    public boolean isShortfallPay(String gopayOrderId) {
        CpsGenMainOrder mainOrder = cpsGeneralMainOrderQueryDAO.find(CpsGenMainOrder.class, gopayOrderId);
        CpsGenMainOrder chargeMainOrder = cpsGeneralMainOrderQueryDAO.find(CpsGenMainOrder.class, mainOrder.getRelevanceGopayOrderId());
        if(mainOrder.getMerTxnAmt().compareTo(chargeMainOrder.getMerTxnAmt()) == 0){
            return false;
        }
        return true;
    }

	@Override
	public BigDecimal findTotal02RefundUsedLimit(String gopayOrderId) {
		// TODO Auto-generated method stub
		return cpsGeneralReceiptOrderQueryDAO.findTotal02RefundUsedLimit(gopayOrderId);
	}

}
